<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Section 5.2.5: Mixed strategies for matrix games (LP formulation)</title>
<link rel="canonical" href="/Users/mcgrant/Projects/CVX/examples/cvxbook/Ch05_duality/html/matrix_games_LP.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Section 5.2.5: Mixed strategies for matrix games (LP formulation)</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
Plots
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Jo&euml;lle Skaf - 08/24/05</span>
<span class="comment">%</span>
<span class="comment">% Player 1 wishes to choose u to minimize his expected payoff u'Pv, while</span>
<span class="comment">% player 2 wishes to choose v to maximize u'Pv, where P is the payoff</span>
<span class="comment">% matrix, u and v are the probability distributions of the choices of each</span>
<span class="comment">% player (i.e. u&gt;=0, v&gt;=0, sum(u_i)=1, sum(v_i)=1)</span>
<span class="comment">% LP formulation:   minimize    t</span>
<span class="comment">%                       s.t.    u &gt;=0 , sum(u) = 1, P'*u &lt;= t*1</span>
<span class="comment">%                   maximize    t</span>
<span class="comment">%                       s.t.    v &gt;=0 , sum(v) = 1, P*v &gt;= t*1</span>

<span class="comment">% Input data</span>
randn(<span class="string">'state'</span>,0);
n = 12;
m = 12;
P = randn(n,m);

<span class="comment">% Optimal strategy for Player 1</span>
fprintf(1,<span class="string">'Computing the optimal strategy for player 1 ... '</span>);

cvx_begin
    variables <span class="string">u(n)</span> <span class="string">t1</span>
    minimize ( t1 )
    u &gt;= 0;
    ones(1,n)*u == 1;
    P'*u &lt;= t1*ones(m,1);
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Optimal strategy for Player 2</span>
fprintf(1,<span class="string">'Computing the optimal strategy for player 2 ... '</span>);

cvx_begin
    variables <span class="string">v(m)</span> <span class="string">t2</span>
    maximize ( t2 )
    v &gt;= 0;
    ones(1,m)*v == 1;
    P*v &gt;= t2*ones(n,1);
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
disp(<span class="string">'------------------------------------------------------------------------'</span>);
disp(<span class="string">'The optimal strategies for players 1 and 2 are respectively: '</span>);
disp([u v]);
disp(<span class="string">'The expected payoffs for player 1 and player 2 respectively are: '</span>);
[t1 t2]
disp(<span class="string">'They are equal as expected!'</span>);
</pre>
<a id="output"></a>
<pre class="codeoutput">
Computing the optimal strategy for player 1 ...  
Calling SDPT3 4.0: 25 variables, 13 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 13
 dim. of linear var  = 24
 dim. of free   var  =  1 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|7.9e+01|2.0e+01|2.4e+03|-2.875957e-10  0.000000e+00| 0:0:00| chol  1  1 
 1|0.904|0.649|7.5e+00|7.2e+00|6.8e+02| 1.098634e+01 -8.128345e+00| 0:0:00| chol  1  1 
 2|1.000|0.981|2.1e-06|1.4e-01|3.1e+01| 1.147210e+01 -9.969407e+00| 0:0:00| chol  1  1 
 3|1.000|0.815|1.6e-05|2.7e-02|5.5e+00| 2.771734e+00 -2.299250e+00| 0:0:00| chol  1  1 
 4|1.000|0.064|3.9e-07|2.6e-02|3.2e+00| 6.734832e-01 -2.219706e+00| 0:0:00| chol  1  1 
 5|1.000|0.856|1.6e-06|3.7e-03|9.5e-01| 4.523826e-01 -4.802464e-01| 0:0:00| chol  1  1 
 6|0.972|0.412|1.8e-07|2.2e-03|5.5e-01| 1.986797e-01 -3.427067e-01| 0:0:00| chol  1  1 
 7|1.000|0.376|3.0e-07|1.4e-03|3.7e-01| 1.401336e-01 -2.249115e-01| 0:0:00| chol  1  1 
 8|1.000|0.388|7.3e-08|8.4e-04|2.3e-01| 9.168395e-02 -1.355052e-01| 0:0:00| chol  1  1 
 9|1.000|0.404|3.3e-08|5.0e-04|1.4e-01| 7.060838e-02 -6.978558e-02| 0:0:00| chol  1  1 
10|1.000|0.611|9.1e-09|1.9e-04|5.2e-02| 4.979844e-02 -1.873493e-03| 0:0:00| chol  1  1 
11|0.924|0.758|2.3e-09|4.7e-05|1.2e-02| 4.559078e-02  3.325733e-02| 0:0:00| chol  1  1 
12|0.996|0.859|1.5e-10|6.6e-06|1.7e-03| 4.486235e-02  4.319704e-02| 0:0:00| chol  1  1 
13|0.988|0.983|2.1e-11|1.9e-05|3.0e-05| 4.484071e-02  4.481316e-02| 0:0:00| chol  1  1 
14|0.989|0.989|2.7e-13|3.4e-07|3.4e-07| 4.484043e-02  4.484012e-02| 0:0:00| chol  1  1 
15|1.000|0.989|1.2e-14|3.8e-09|7.0e-09| 4.484042e-02  4.484042e-02| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 15
 primal objective value =  4.48404239e-02
 dual   objective value =  4.48404172e-02
 gap := trace(XZ)       = 7.00e-09
 relative gap           = 6.43e-09
 actual relative gap    = 6.14e-09
 rel. primal infeas (scaled problem)   = 1.22e-14
 rel. dual     "        "       "      = 3.77e-09
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 7.4e-01, 4.4e-01, 9.7e-01
 norm(A), norm(b), norm(C) = 1.4e+01, 2.0e+00, 2.4e+00
 Total CPU time (secs)  = 0.15  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.2e-14  0.0e+00  4.6e-09  0.0e+00  6.1e-09  6.4e-09
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
 
Done! 
Computing the optimal strategy for player 2 ...  
Calling SDPT3 4.0: 25 variables, 13 equality constraints
   For improved efficiency, SDPT3 is solving the dual problem.
------------------------------------------------------------

 num. of constraints = 13
 dim. of linear var  = 24
 dim. of free   var  =  1 *** convert ublk to lblk
*******************************************************************
   SDPT3: Infeasible path-following algorithms
*******************************************************************
 version  predcorr  gam  expon  scale_data
    NT      1      0.000   1        0    
it pstep dstep pinfeas dinfeas  gap      prim-obj      dual-obj    cputime
-------------------------------------------------------------------
 0|0.000|0.000|7.5e+01|2.0e+01|2.4e+03|-2.875957e-10  0.000000e+00| 0:0:00| chol  1  1 
 1|0.903|0.656|7.3e+00|7.1e+00|6.8e+02| 1.084223e+01 -8.279551e+00| 0:0:00| chol  1  1 
 2|1.000|0.981|2.5e-06|1.5e-01|3.2e+01| 1.144905e+01 -1.008569e+01| 0:0:00| chol  1  1 
 3|1.000|0.806|1.5e-05|2.9e-02|5.8e+00| 2.825809e+00 -2.488865e+00| 0:0:00| chol  1  1 
 4|1.000|0.067|8.2e-07|2.8e-02|3.3e+00| 6.379819e-01 -2.400851e+00| 0:0:00| chol  1  1 
 5|0.915|0.884|1.6e-06|3.2e-03|5.4e-01| 1.130049e-01 -4.134015e-01| 0:0:00| chol  1  1 
 6|1.000|0.652|9.5e-08|1.1e-03|3.5e-01| 1.157364e-01 -2.254496e-01| 0:0:00| chol  1  1 
 7|0.933|0.584|1.6e-07|4.7e-04|1.1e-01|-1.533932e-02 -1.260174e-01| 0:0:00| chol  1  1 
 8|0.942|0.375|4.8e-08|2.9e-04|6.2e-02|-3.640272e-02 -9.820508e-02| 0:0:00| chol  1  1 
 9|1.000|0.529|1.7e-08|1.4e-04|2.9e-02|-4.200414e-02 -7.055158e-02| 0:0:00| chol  1  1 
10|1.000|0.575|2.4e-09|5.8e-05|1.2e-02|-4.410085e-02 -5.584656e-02| 0:0:00| chol  1  1 
11|1.000|0.637|5.6e-10|2.1e-05|4.2e-03|-4.475713e-02 -4.888417e-02| 0:0:00| chol  1  1 
12|0.989|0.963|7.6e-11|7.9e-07|1.5e-04|-4.483902e-02 -4.499178e-02| 0:0:00| chol  1  1 
13|0.989|0.988|3.3e-12|1.7e-06|1.9e-06|-4.484041e-02 -4.484217e-02| 0:0:00| chol  1  1 
14|1.000|0.989|3.3e-14|2.2e-08|4.1e-08|-4.484041e-02 -4.484045e-02| 0:0:00| chol  1  1 
15|1.000|0.989|1.6e-15|4.6e-10|7.7e-10|-4.484042e-02 -4.484042e-02| 0:0:00|
  stop: max(relative gap, infeasibilities) &lt; 1.49e-08
-------------------------------------------------------------------
 number of iterations   = 15
 primal objective value = -4.48404220e-02
 dual   objective value = -4.48404227e-02
 gap := trace(XZ)       = 7.68e-10
 relative gap           = 7.05e-10
 actual relative gap    = 6.70e-10
 rel. primal infeas (scaled problem)   = 1.60e-15
 rel. dual     "        "       "      = 4.60e-10
 rel. primal infeas (unscaled problem) = 0.00e+00
 rel. dual     "        "       "      = 0.00e+00
 norm(X), norm(y), norm(Z) = 9.7e-01, 4.0e-01, 7.4e-01
 norm(A), norm(b), norm(C) = 1.4e+01, 2.0e+00, 2.4e+00
 Total CPU time (secs)  = 0.14  
 CPU time per iteration = 0.01  
 termination code       =  0
 DIMACS: 1.6e-15  0.0e+00  5.5e-10  0.0e+00  6.7e-10  7.1e-10
-------------------------------------------------------------------
 
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0.0448404
 
Done! 
------------------------------------------------------------------------
The optimal strategies for players 1 and 2 are respectively: 
    0.2695    0.0686
    0.0000    0.1619
    0.0973    0.0000
    0.1573    0.2000
    0.1145    0.0000
    0.0434    0.1545
    0.0000    0.1146
    0.0000    0.0000
    0.2511    0.1030
    0.0670    0.0000
    0.0000    0.0000
    0.0000    0.1974

The expected payoffs for player 1 and player 2 respectively are: 

ans =

   -0.0448   -0.0448

They are equal as expected!
</pre>
</div>
</body>
</html>